package com.example.student.service.impl;

import com.example.student.constant.ResultCode;
import com.example.student.dao.UserDAO;
import com.example.student.entity.ResultDO;
import com.example.student.entity.UserDO;
import com.example.student.service.UserInterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;

/**
 * 用户逻辑实现
 *
 * @author
 */
@ComponentScan({"com.example.student.dao"})
@Service("userService")
public class UserServiceImpl implements UserInterService {

    @Resource
    private UserDAO userDAO;

    @Override
    public ResultDO<Void> login(UserDO userDO) {
        // 参数检验
        if (StringUtils.isBlank(userDO.getLoginName()) || StringUtils.isBlank(userDO.getPassword())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }

        // 检测
        UserDO result = userDAO.login(userDO.getLoginName(), DigestUtils.md5DigestAsHex(userDO.getPassword().getBytes()));
        if (result == null) {
            // 用户名或者密码错误
            return new ResultDO<>(false, ResultCode.WRONG_USERNAME_OR_PASSWORD, ResultCode.MSG_WRONG_USERNAME_OR_PASSWORD, null);
        }

        // 登陆成功
        return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, null);
    }

}